import { _decorator, Component, Node ,Camera,Sprite,RenderTexture,SpriteFrame,tween,Vec3,v3} from 'cc';
import {GameMain} from "./GameMain";
import Platform, {PlatformType} from "./Platform";
const { ccclass, property } = _decorator;

@ccclass('TTShareUI')
export class TTShareUI extends Component
{
    @property({type:Node})
    targetNode:Node=null;

    @property({type:Camera})
    rttCamera:Camera=null;

    @property({type:Sprite})
    rttSprite:Sprite=null;

    public scoreVal:number=0;

    //rtt设置为静态对象确保只创建一次
    public static renderTexture: RenderTexture = null;
    public static rttSpriteFrame: SpriteFrame = null;

    onLoad()
    {
        //this.rttInit();
    }

    rttInit()
    {
        if (TTShareUI.rttSpriteFrame === null)
        {
            TTShareUI.rttSpriteFrame = new SpriteFrame();
            TTShareUI.rttSpriteFrame.reset({
                originalSize: this.rttSprite.spriteFrame.getOriginalSize(),
                rect: this.rttSprite.spriteFrame.getRect(),
                offset: this.rttSprite.spriteFrame.getOffset(),
                isRotate: this.rttSprite.spriteFrame.isRotated(),
                borderTop: this.rttSprite.spriteFrame.insetTop,
                borderLeft: this.rttSprite.spriteFrame.insetLeft,
                borderBottom: this.rttSprite.spriteFrame.insetBottom,
                borderRight: this.rttSprite.spriteFrame.insetRight,
            });
        }

        if (TTShareUI.renderTexture === null)
        {
            TTShareUI.renderTexture = new RenderTexture();
            TTShareUI.renderTexture.reset({
                width: 512,
                height: 512
            });
        }

        this.rttCamera.targetTexture = TTShareUI.renderTexture;
        TTShareUI.rttSpriteFrame.texture = TTShareUI.renderTexture;
        this.rttSprite.spriteFrame = TTShareUI.rttSpriteFrame;
    }

    public onShareClicked()
    {
        if(Platform.currentPlatform===PlatformType.TT)
        {
            Platform.shareRecord(() =>
            {
                this.scheduleOnce(()=>{
                    this.close();
                }, 2);

                Platform.showToast("分享成功 !");
            });
        }
    }

    public show(scoreVal:number)
    {
    }

    public close()
    {
    }

}

